<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_tb_ipModel extends AppModel
{   
    private $ip = '119.29.136.114';
    public function getAliAppIdByIp($ip)
    {
        return $this->field('ali_app_id')->find(['ip' => $ip])['ali_app_id'];
    }

    public function getActiveApp()
    {
        $tables = [
            'app.ali_app'
        ];
        $fields = [
            $this->table . '.*',
            'ali_app.app'
        ];
        $condition = [];
        $condition[] = ['status' => 1];
        $condition[] = ['ali_app_id' => ['eq', 'ali_app.id', 'AND', true]];
        return $this->table($tables)->field($fields)->find($condition);
    }

    public function getAppName($ip)
    {
        $tables = [
            'app.ali_app',
        ];
        $fields = [
            $this->table . '.*',
            'ali_app.app'
        ];
        $condition = [];
        $condition[] = ['ip' => $ip];
        $condition[] = ['ali_app_id' => ['eq', 'ali_app.id', 'AND', true]];

        return $this->table($tables)->field($fields)->find($condition);
    }

    public function isActive($ip, $app)
    {
        $tables = [
            'app.ali_app',
        ];
        $fields = [
            'id',
            'ali_app.app'
        ];
        $condition = [];
        $condition[] = ['status' => 1];
        $condition[] = ['ip' => $ip];
        $condition[] = ['ali_app.app' => $app];
        $condition[] = ['ali_app_id' => ['eq', 'ali_app.id', 'AND', true]];

        return $this->table($tables)->field($fields)->find($condition);
    }

    public function isActiveByIp($ip)
    {
        return $this->field('status')->find(['ip' => $ip,'status'=>1]);
    }
    public function getIPByAliAppId($ali_app_id = 4, $status = 1)
    {
        if (!$this->select(['ali_app_id' => $ali_app_id])) {
            return $this->ip;
        }
        $ip = $this->field('ip')->order('id asc')->find(['ali_app_id' => $ali_app_id, 'status' => 1])['ip'];
        if (!empty($ip)) {
            return $ip;
        } else {
            return $this->ip;
        }
    }
}
